<template>
  <div class="container">
    <div class="app-container">
      <!-- 添加权限按钮 -->
      <div>
        <el-button type="primary" size=" mini" class="btn-add" @click="addPermission(0,1)">添加权限</el-button>
      </div>
      <!-- table表格 -->
      <el-table :data="tableData" row-key="id" :border="true">
        <el-table-column label="名称" prop="name" align="center"></el-table-column>
        <el-table-column label="标识" prop="code" align="center"></el-table-column>
        <el-table-column label="描述" prop="description" align="center"></el-table-column>
        <el-table-column label="操作" >
          <template v-slot="{row}">
            <el-button v-if="row.type ===1" size="mini" type="text" @click="addPermission(row.id,2)">添加</el-button>
            <el-button size="mini" type="text" @click= updatePermission(row.id) >编辑</el-button>
            <el-button size="mini" type="text" @click="deletePermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加弹框 -->
    <div>
      <el-dialog :visible.sync="dialogVisible" :title="`${showTitle}权限点`" width="1000px" >
          <el-form ref="addform" :model ="userInfo" :rules="rulesForm">
            <el-form-item label="权限名称" prop="name" label-width="120px">
              <el-input v-model="userInfo.name"/>
            </el-form-item>
            <el-form-item label="权限标识" prop="code" label-width="120px">
              <el-input v-model="userInfo.code"/>
            </el-form-item>
            <el-form-item label="权限描述" prop="description" label-width="120px">
              <el-input v-model="userInfo.description"/>
            </el-form-item>
            <el-form-item label="开启" label-width="120px">
              <el-switch :active-value="0" :inactive-value="1" v-model="userInfo.enVisible"></el-switch>
            </el-form-item>
            <el-row >
              <el-col type="flex" align="middle" justify="center">
                <el-button type="primary" @click="btnOk">确认</el-button>
                <el-button @click="btnCenter" >取消</el-button>
              </el-col>
            </el-row>
          </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {getPermissionList ,addPermission ,deletePermission,getPermissionById} from '@/api/permission'
import {transListToTreeData} from '@/utils/index'
import { computed } from 'vue'
export default {
  name: 'Permission',
  data(){
    return{
      //表格的列表数据
      tableData:[],
      //弹框的控制框
      dialogVisible:false,
      //表单中的数据
      userInfo:{
        name:'',//权限名称
        code:'',//权限标识
        description:'',//权限描述
        pid:'',//父id
        type:'',//类型
        enVisible:0 //开启
      },
      //表单数据验证
      rulesForm:{
        name:[{required:true,message:'请输入权限名称',trigger: 'blur'}],//权限名称
        code:[{required:true,message:'请输入权限标识',trigger: 'blur'}],//权限标识
        description:[{required:true,message:'请输入权限描述',trigger: 'blur'}]//权限描述
      }
    }
  },
  methods:{
    //表格的列表数据
    async getPermissionList(){
      this.tableData = transListToTreeData(await getPermissionList(),0)
    },
    //弹框控制
    addPermission(pid,type){
      this.dialogVisible = true
      this.userInfo.pid = pid
      this.userInfo.type = type
    },
    //确认
    btnOk(){
      //验证数据
      this.$refs.addform.validate(async (isOk)=>{
        if(isOk){
          if(this.userInfo.id){
            updatePermission(this.userInfo)
            this.$message.success('编辑权限成功')
          }else{
            await addPermission(this.userInfo)
            this.$message.success('添加权限成功')
          }
        }
        this.btnCenter()
        this.getPermissionList()
      })
    },
    //取消
    btnCenter(){
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: "0" // 默认关闭
      }
      this.dialogVisible = false
      this.$refs.addform.resetFields()
    },
    //删除
    async deletePermission(id){
      try {
        await this.$confirm('确定要删除该数据吗')
        await deletePermission(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    //编辑回显数据
    async updatePermission(id){
      this.dialogVisible =true
      this.userInfo = await getPermissionById(id)
    },
    
  },
  //显示标题
  computed:{
      showTitle(){
        return this.userInfo.id ?"编辑":"新增"
      }
    },
  created(){
    this.getPermissionList()
  }
}
</script>
<style scoped>
.btn-add{
  margin: 10px;
}
</style>
